import { API_LIST } from '../../../Api/Api'
import { MenuConversion } from '../Common/Conversion'
import { AddDataButton, ModDataButton, DelDataButton } from '../Common/options'
import { commonTableList, BuiltInData } from '../Common/common'

export const Menu = async ({ url, OptionsEvent, ClientData }) => {
    const parentUuid = await MenuConversion()

    let data = {
        type: '系统配置',
        pageSize: 10000,
        params: { pageIndex: 1 },
        get: API_LIST()['getMenuWebTree'],
        added: `pos${url}`,
        modify: `put${url}`,
        delete: `del${url}`,
        hideInSearch: true,
        options: [<AddDataButton OptionsEvent={OptionsEvent} url={url} type="primary" icon="add" title="新增菜单" />],
        columns: [
            commonTableList.uuid,
            { ...commonTableList.tableName, title: '菜单名称', width: 300 },
            { ...commonTableList.sortId, rules: true },
            {
                ...commonTableList.parentUuid,
                fieldProps: {
                    fieldNames: { label: 'tableName', value: 'uuid' },
                    options: parentUuid,
                },
            },
            {
                dataIndex: 'type',
                title: '类型',
                width: 100,
                rules: true,
                valueType: 'select',
                valueEnum: BuiltInData.MenuType,
                hideInSearch: true,
            },
            {
                dataIndex: 'targetUrl',
                title: '页面地址',
                valueType: 'input',
                rules: true,
                width: 200,
                valueJudge: 'type',
                JudgeValue: '0',
                hideInTable: true,
            },
            {
                dataIndex: 'pageType',
                title: '页面类型',
                valueType: 'select',
                valueEnum: ClientData.dictionary['1a7f3576-6287-4d0c-a408-baacb46f05a8'],
                rules: true,
                valueJudge: 'type',
                JudgeValue: '0',
                hideInTable: true,
            },
            { ...commonTableList.isEnabled, rules: true },
            {
                dataIndex: 'icon',
                title: 'icon',
                valueType: 'ICON',
                valueJudge: 'parentUuid',
                JudgeValue: '00000000-0000-0000-0000-000000000000',
                rules: true,
                hideInSearch: true,
                hideInTable: true,
            },
            commonTableList.remark,
            commonTableList.createUserName,
            commonTableList.createTime,
            commonTableList.updateUserName,
            commonTableList.updateTime,
            {
                title: '操作',
                valueType: 'systemOperation',
                fixed: 'right',
                width: 300,
                hideInSearch: true,
                hideInAdded: true,
                hideInModify: true,
                render: (_, record) => {
                    const { uuid, type, children, targetUrl } = record

                    return [
                        targetUrl !== 'Home' && type !== 1 && (
                            <AddDataButton
                                OptionsEvent={OptionsEvent}
                                key="addMenu"
                                url={url}
                                title="追加"
                                AddTo={{ key: 'parentUuid', uuid }}
                            />
                        ),
                        <ModDataButton OptionsEvent={OptionsEvent} key="modify" title="修改" url={url} data={record} />,
                        targetUrl !== 'Home' && !children && (
                            <DelDataButton
                                OptionsEvent={OptionsEvent}
                                key="delete"
                                title="删除"
                                url={url}
                                uuid={record.uuid}
                            />
                        ),
                    ]
                },
            },
        ],
    }
    return data
}
